namespace BM.Tools.WebControls.MForm.Generator
{
    using System.Xml.Schema;

    using BM.Tools.Xml.Schema;

    /// <summary>
    /// FormElement is an intermediate class that is created after visiting <see cref="XmlSchemaElement" /> node.
    /// </summary>
    public class FormElement : FormItem
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FormElement"/> class. 
        /// </summary>
        /// <param name="builder">
        /// The builder.
        /// </param>
        /// <param name="parent">
        /// The parent.
        /// </param>
        /// <param name="schemaElement">
        /// The schemaElement.
        /// </param>
        public FormElement(FormBuilder builder, Visited parent, XmlSchemaElement schemaElement) : base(builder, parent, schemaElement)
        {
            SchemaElement = schemaElement;
            DefaultValue = schemaElement.DefaultValue;
            FixedValue = schemaElement.FixedValue;
        }

        /// <summary>
        /// Gets MinOccurs.
        /// </summary>
        /// <value>
        /// The min occurs.
        /// </value>
        public override decimal MinOccurs
        {
            get
            {
                if (Builder.Settings.IgnoreNonLeafMinOccurs && !IsLeaf)
                {
                    return 0;
                }

                if (Builder.Settings.IgnoreLeafMinOccurs && IsLeaf)
                {
                    return 0;
                }

                return base.MinOccurs;
            }
        }

        /// <summary>
        /// Gets SchemaElement.
        /// </summary>
        /// <value>
        /// The schema element.
        /// </value>
        public XmlSchemaElement SchemaElement
        {
            get;
            private set;
        }
    }
}